package com.anivia.common.web.config.tenant;

import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import lombok.extern.slf4j.Slf4j;

/**
 * 租户上下文
 *
 * @author wangj
 */
@Slf4j
public class TenantContext {
    private static final ThreadLocal<Boolean> DATA_SCOPE = new InheritableThreadLocal<>();

    /**
     * 开始忽略租户
     */
    public static void ignore() {
        InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
        DATA_SCOPE.set(true);
    }

    /**
     * 停止忽略租户
     */
    public static void close() {
        InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
        DATA_SCOPE.set(false);
    }

    public static boolean isIgnore() {
        if (DATA_SCOPE.get() == null) {
            return false;
        }
        return DATA_SCOPE.get();
    }
}
